<?php
if (isset($_REQUEST["sendquery"]))
{
	$success = true;
	$validator = array("bookingtimefrom" => "isPostedDatetime",
						"bookingtimeto" => "isPostedDatetime",
						"amountfrom" => "isInteger",
						"amountto" => "isInteger",
						"accountid" => "isNotEmpty",
						"category" => "isNotEmpty");
	if ($success = validateForm($validator, $_REQUEST))
	{
		$transactionTree = getTransactionTree($_SESSION["userid"], $_REQUEST["accountid"], $_REQUEST["bookingtimefrom"], $_REQUEST["bookingtimeto"], $_REQUEST["amountfrom"], $_REQUEST["amountto"], $_REQUEST["category"], $_REQUEST["partnername"], $_REQUEST["freetext"], $_REQUEST["tag"], $_REQUEST["direction"]);
	}
}

$accounts = getAccounts($_SESSION["userid"]);
$categoryTree = getCategoryTree("NULL", 1, false, true);
?>
			<div id="textcontainer">
				<h2>Tranzakció lista</h2>
				<p>
					<form name="translist" action="<?php print $_SERVER["SCRIPT_NAME"] . "?" . $_SERVER["QUERY_STRING"];?>" method="post">
						számla: <select name="accountid[]" multiple="multiple">
						<option value="all">minden számla</option>
<?php
	foreach ($accounts as $acc)
	{
		print "<option value=\"" . $acc["id"] . "\"" . (isset($_REQUEST["accountid"]) && (in_array($acc["id"], $_REQUEST["accountid"]))?" selected=\"selected\"":"") . ">" . $acc["name"] . "</option>";
	}
?>
						</select><br/>
						összeg: <input type="text" name="amountfrom"<?php print (isset($_REQUEST["amountfrom"]))?' value="' . $_REQUEST["amountfrom"] . '"':" value=\"0\"";?>/> - <input type="text" name="amountto"<?php print (isset($_REQUEST["amountto"]))?' value="' . $_REQUEST["amountto"] . '"':"";?>/><br/>
						partner: <input type="text" name="partnername"<?php print (isset($_REQUEST["partnername"]))?' value="' . $_REQUEST["partnername"] . '"':"";?>/><br/>
						szöveg: <input type="text" name="freetext"<?php print (isset($_REQUEST["freetext"]))?' value="' . $_REQUEST["freetext"] . '"':'';?>/><br/>
						időpont: <input type="text" name="bookingtimefrom"<?php print (isset($_REQUEST["bookingtimefrom"]))?" value=\"" . $_REQUEST["bookingtimefrom"] . "\"":"";?>/> - <input type="text" name="bookingtimeto"<?php print (isset($_REQUEST["bookingtimeto"]))?" value=\"" . $_REQUEST["bookingtimeto"] . "\"":"";?>/><i>(pl. 2005-10-12 15:00:00)</i><br/>
						címke: <input type="text" name="tag"<?php print (isset($_REQUEST["tag"]))?" value=\"" . $_REQUEST["tag"] . "\"":"";?>/><br/>
						<select name="direction"><option value="B" <?php print (!isset($_REQUEST["direction"]) || $_REQUEST["direction"] == "B")?" selected=\"selected\"":"";?>>mindkettő</option><option value="E"<?php print (isset($_REQUEST["direction"]) && $_REQUEST["direction"] == "E")?" selected=\"selected\"":"";?>>kiadás</option><option value="I"<?php print (isset($_REQUEST["direction"]) && $_REQUEST["direction"] == "I")?" selected=\"selected\"":"";?>>bevétel</option></select>
						<ul>
<?php
	foreach ($categoryTree as $category)
	{
		print "<li>";
		print "<input type=\"checkbox\" name=\"category[]\" value=\"" . $category["category"]["id"] . "\"" . (isset($_REQUEST["category"]) && in_array($category["category"]["id"], $_REQUEST["category"])?" checked=\"checked\"":"") . "/> " . $category["category"]["name"];
		if (isset($category["subcategoryList"]))
		{
			print "<ul>";
			foreach($category["subcategoryList"] as $subcategory)
			{
				print "<li><input type=\"checkbox\" name=\"category[]\" value=\"" . $subcategory["category"]["id"] . "\"" . (isset($_REQUEST["category"]) && in_array($subcategory["category"]["id"], $_REQUEST["category"])?" checked=\"checked\"":"") . "/> " . $subcategory["category"]["name"] . "</li>";
			}
			print "</ul>";
		}
		print "</li>";
	}
?>
						</ul><br/>
						<input type="submit" name="sendquery" value="Keres"/>
					</form>
<?php
if (isset($success))
{
	if (!$success) print "<div class=\"errortitle\">hiba</div>";
	else
	{
		if (count($transactionTree) == 0) print "<div class=\"errortitle\">nincs eredmény</div>";
		else
		{
			print '<table>';
			print '<tr><th>időpont</th><th>számla</th><th>összeg</th><th>kategória</th><th>leírás</th><th>partner</th><th>címkék</th><th></th></tr>';
			foreach ($transactionTree as $t)
			{
				print '<tr bgcolor="#' . (($t["amount"] > 0)?"aaffaa":"ffaaaa") . '"><td>' . $t["bookingtime"] . '</td><td>' . $t["accountname"] . '</td><td>' . abs($t["amount"]) . '</td><td>' . getCategoryString($categoryTree, $t["categoryid"]) . '</td><td>' . $t["description"] . '</td><td>' . $t["partnername"] . '</td><td>' . $t["tags"] . '</td><td><a href="' . $_SERVER["SCRIPT_NAME"] . '?p=modtrans&id=' . $t["id"] . '">szerkeszt</a> | <a href="' . $_SERVER["SCRIPT_NAME"] . '?p=splittrans&id=' . $t["id"] . '">feloszt</a></td></tr>';
			}
			print '</table>';
		}
	}
}
?>
				</p>
			</div>
			<!---->
